package com.example.my.meila.view.activity;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alipay.sdk.app.PayTask;
import com.example.my.meila.R;
import com.example.my.meila.entity.Login;
import com.example.my.meila.entity.User;
import com.example.my.melia.greendao.DaoMaster;
import com.example.my.melia.greendao.DaoSession;
import com.example.my.melia.greendao.UserDao;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.greendao.query.Query;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;


public class LoginActivity extends AppCompatActivity {


    @BindView(R.id.et1)
    EditText et1;
    @BindView(R.id.et2)
    EditText et2;
    @BindView(R.id.tv_reallogin)
    TextView tvReallogin;
    @BindView(R.id.tv_real)
    TextView tvReal;
    @BindView(R.id.iv_xinlang)
    ImageView ivXinlang;
    @BindView(R.id.tv_zhifu)
    TextView tvzhifu;
    private UserDao userDao;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);


        DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(this, "testGreen", null);
        SQLiteDatabase db = devOpenHelper.getReadableDatabase();
        DaoMaster master = new DaoMaster(db);
        DaoSession daoSession = master.newSession();
        userDao = daoSession.getUserDao();
    }

    @OnClick({R.id.tv_reallogin, R.id.tv_real, R.id.iv_xinlang, R.id.tv_zhifu})
    public void onClick(View view) {
        String phone = et1.getText().toString();
        String psw = et2.getText().toString();
        Query query = userDao.queryBuilder().where(UserDao.Properties.Phone.eq(phone)).build();
        List<User> list = query.list();
        switch (view.getId()) {
            case R.id.tv_reallogin:
                if (list.size() == 0) {
                    Toast.makeText(this, "您还没注册", Toast.LENGTH_SHORT).show();
                } else {
                    if (psw.equals(list.get(0).getPsw())) {
                        Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                        EventBus.getDefault().post("123");
                        finish();
                    } else {
                        Toast.makeText(this, "密码错误，登录失败", Toast.LENGTH_SHORT).show();
                    }
                }
                break;
            case R.id.tv_real:

                if (phone.isEmpty() || psw.isEmpty()) {
                    Toast.makeText(this, "账号和密码不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    if (list.size() == 0) {
                        Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
                        User user = new User(null, phone, psw);
                        userDao.insert(user);
                    } else {
                        Toast.makeText(this, "账户已经存在", Toast.LENGTH_SHORT).show();
                    }
                }
                break;
            case R.id.iv_xinlang:
                thirdSinaLogin();
                break;
            case R.id.tv_zhifu:
                final String orderInfo = "";
                new Thread() {
                    @Override
                    public void run() {
                        super.run();

                        PayTask task = new PayTask(LoginActivity.this);
                        Map<String, String> pay = task.payV2(orderInfo, true);
                    }
                }.start();
                break;
        }

    }

    private void thirdSinaLogin() {
        //初始化新浪平台
        Platform pf = ShareSDK.getPlatform(this, SinaWeibo.NAME);
        pf.SSOSetting(true);
        //设置监听
        pf.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                EventBus.getDefault().post(new Login(platform.getDb().getUserName(), platform.getDb().getUserIcon()));
                EventBus.getDefault().post("123");
                finish();
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {

            }

            @Override
            public void onCancel(Platform platform, int i) {

            }
        });
        //获取登陆用户的信息，如果没有授权，会先授权，然后获取用户信息
        pf.authorize();
    }

}